package com.glory.hikvisionmobile.activity;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.LotPauseOrGo.LotPauseBean;
import com.glory.hikvisionmobile.bean.LotPauseOrGo.PauseGroupBean;
import com.glory.hikvisionmobile.bean.LotPauseOrGo.PdaUserGroup;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.EditTextEnterUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class Lot_LotPauseActivity extends BaseActivity {

    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvPauseCode)
    TextView tvPauseCode;
    @BindView(R.id.ivPauseCodeListArrow)
    ImageView ivPauseCodeListArrow;
    @BindView(R.id.flPauseCodeList)
    FrameLayout flPauseCodeList;
    @BindView(R.id.etPauseReason)
    FreeUI_ClearEditText etPauseReason;
    @BindView(R.id.tvPauseGroup)
    TextView tvPauseGroup;
    @BindView(R.id.ivPauseGroupListArrow)
    ImageView ivPauseGroupListArrow;
    @BindView(R.id.flPauseGroupList)
    FrameLayout flPauseGroupList;
    @BindView(R.id.tvStepDetails)
    TextView tvStepDetails;
    @BindView(R.id.tvRecipe)
    TextView tvRecipe;
    @BindView(R.id.tvProductDetails)
    TextView tvProductDetails;
    @BindView(R.id.tvLotNumber)
    TextView tvLotNumber;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvProductName)
    TextView tvProductName;
    @BindView(R.id.tvRecipeDesc)
    TextView tvRecipeDesc;
    private FreeUI_GeneralPop codePop;
    private FreeUI_GeneralPop groupPop;
    private int selectCodePosition = -1;
    private int selectGroupPosition = -1;
    private String pauseCode;
    private String pauseGroup;
    private LotPauseBean lotPauseBean;
    private List<String> groupList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_pause;
    }

    @Override
    protected void initView() {
        initEdit(etLot, s -> requestListForNet(s,"HIK.PdaLotHold"));
    }

    private void requestListForNet(String lotID,String messageName) {
        JsonObject map = new JsonObject();
        map.addProperty("lotId",lotID);
        map.addProperty("actionType","");

        NetUtils.requestDara(messageName,this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<LotPauseBean>>(
                new TypeToken<BaseResponseBean<LotPauseBean>>() {
                }.getType(), getSupportFragmentManager(), this){

            @Override
            public void onSuccess(Response<BaseResponseBean<LotPauseBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())) {
                    lotPauseBean = response.body().getBody();
                    tvLotNumber.setText(lotPauseBean.getMainQty());
                    tvStepDetails.setText(lotPauseBean.getStepDesc());
                    tvRecipe.setText(lotPauseBean.getRecipeName());
                    tvProductDetails.setText(lotPauseBean.getPartDesc());
                    tvProductName.setText(lotPauseBean.getPartName());
                    tvRecipeDesc.setText(lotPauseBean.getRecipeDesc());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LotPauseBean>> response) {
                super.onError(response);
                loadError(response.getException(),messageName);
            }
        });
    }

    @Override
    protected void initData() {
        NetUtils.getSelectorList("HoldCode","USER",this,getSupportFragmentManager(),new NetUtils.SelectorCallback(){

            @Override
            public void onSuccess(List<String> list) {
                if (codePop == null) {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPauseCodeList.getWidth(), list, ivPauseCodeListArrow, (codeS, i) -> {
                        tvPauseCode.setText(codeS);
                        pauseCode = codeS.split("/")[0];
                        selectCodePosition = i;

                    });

                }
            }

            @Override
            public void onFail(Throwable e,String messageName) {
                loadError(e,messageName);
            }
        });

        getPauseGroup();


    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("批次暂停");
        freeUI_titleView.setRightListener("刷新", v -> {
            initPermission("Refresh", this, new ActionControlListener() {
                @Override
                public void onControlChangeListener() {
                    if (editIsEmpty(etLot, "请扫描批次号")) {
                        requestListForNet(etLot.getText().toString().trim(),"HIK.PdaLotHold");
                        codePop = null;
                        groupPop = null;
                        selectGroupPosition = -1;
                        selectCodePosition = -1;
                        tvPauseCode.setText("");
                        tvPauseGroup.setText("");
                        etPauseReason.setText("");
                        initData();
                    }
                }
            });

        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void getPauseGroup(){
        NetUtils.requestDara("HIK.PdaUserGroup",this,new JsonObject(),new FreeUI_DialogEntityCallBack<BaseResponseBean<PauseGroupBean>>(
                new TypeToken<BaseResponseBean<PauseGroupBean>>() {
                }.getType(), getSupportFragmentManager(), this){

            @Override
            public void onSuccess(Response<BaseResponseBean<PauseGroupBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())) {
                    List<PdaUserGroup> pdaUserGroups = response.body().getBody().getxPdaUserGroups();
                    groupList.clear();
                    for (PdaUserGroup group : pdaUserGroups){
                        groupList.add(group.getGroupName() + "/" + group.getGroupDesc());
                    }

                    if (groupPop == null) {
                        groupPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPauseGroupList.getWidth(), groupList, ivPauseGroupListArrow, (str, i) -> {
                            tvPauseGroup.setText(str);
                            pauseGroup = str.split("/")[0];
                            selectGroupPosition = i;

                        });

                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PauseGroupBean>> response) {
                super.onError(response);
                loadError(response.getException(),"HIK.PdaUserGroup");
            }
        });
    }


    @OnClick({R.id.flPauseCodeList, R.id.flPauseGroupList, R.id.tvReplay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.flPauseCodeList:
                if (codePop == null) {
                    showToast("未请求到暂停码列表", false);
                    return;
                }
                if (codePop.isShowing()) {
                    codePop.dismiss();
                } else {
                    FreeApi_PopUtils.setArrowShow(ivPauseCodeListArrow);
                    codePop.showAsDropDown(flPauseCodeList, 0, 0);
                }
                break;
            case R.id.flPauseGroupList:
                if (groupPop == null) {
                    showToast("未请求到暂停责任组列表", false);
                    return;
                }
                if (groupPop.isShowing()) {
                    groupPop.dismiss();
                } else {
                    FreeApi_PopUtils.setArrowShow(ivPauseGroupListArrow);
                    groupPop.showAsDropDown(flPauseGroupList, 0, 0);
                }
                break;
            case R.id.tvReplay:
                initPermission("Submit", this, new ActionControlListener() {
                    @Override
                    public void onControlChangeListener() {
                        String pauseReason = etPauseReason.getText().toString().trim();
                        String lot = etLot.getText().toString().trim();
                        if (FreeApi_StringUtils.isEmpty(lot)) {
                            showToast("请先扫描批次号", false);
                            return;

                        }
                        if (selectCodePosition == -1){
                            showToast("暂停码不能为空",false);
                            return;
                        }

                        if (selectGroupPosition == -1){
                            showToast("暂停责任组不能为空",false);
                            return;
                        }
                        if (FreeApi_StringUtils.isEmpty(pauseReason)) {
                            showToast("暂停原因不能为空", false);
                            return;
                        }
                        replayForNet(lot, pauseReason, pauseCode, pauseGroup,"HIK.PdaLotHold");
                    }
                });

                break;
        }
    }

    private void replayForNet(String lotID, String pauseReason, String pauseCode, String pauseGroup,String messageName) {
        DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, new DialogUtils.signCallback() {
            @Override
            public void onSignSuccess() {
                JsonObject map = new JsonObject();
                map.addProperty("lotId",lotID);
                map.addProperty("actionType","On");
                map.addProperty("holdOwner",pauseGroup);
                map.addProperty("holdCode",pauseCode);
                map.addProperty("holdReason",pauseReason);

                NetUtils.requestDara(messageName,this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<LotPauseBean>>(
                        new TypeToken<BaseResponseBean<LotPauseBean>>() {
                        }.getType(), getSupportFragmentManager(), this){

                    @Override
                    public void onSuccess(Response<BaseResponseBean<LotPauseBean>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())) {
                            showConfirmDialog(Lot_LotPauseActivity.this,"暂停成功！");
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<LotPauseBean>> response) {
                        super.onError(response);
                        loadError(response.getException(),messageName);
                    }
                });
            }
        }, getSupportFragmentManager());

    }

}
